package com.two.lbms.hjx.service;

import com.two.lbms.base.BaseService;
import com.two.lbms.dao.BookInfoMapper;
import com.two.lbms.dao.DictionaryListMapper;
import com.two.lbms.dao.LendListMapper;
import com.two.lbms.dao.RenewListMapper;
import com.two.lbms.hjx.dto.BookDTO;
import com.two.lbms.hjx.model.BookQuery;
import com.two.lbms.hjx.model.RenewQuery;
import com.two.lbms.syt.dto.LendDTO;
import com.two.lbms.utils.AssertUtil;
import com.two.lbms.utils.CookieUtil;
import com.two.lbms.vo.BookInfo;
import com.two.lbms.vo.DictionaryList;
import com.two.lbms.vo.LendList;
import com.two.lbms.vo.RenewList;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * @author hjx
 * @version V1.0
 * @Date 2023/8/2 16:49
 */
@Service
@Slf4j
public class BookInfoService extends BaseService<BookInfo,Integer> {

    @Resource
    private BookInfoMapper bookInfoMapper;
    @Resource
    private DictionaryListMapper dictionaryListMapper;
    @Resource
    private LendListMapper lendListMapper;
    @Resource
    private RenewListMapper renewListMapper;

    @Transactional(propagation = Propagation.REQUIRED)
    public void addBook(BookQuery bookQuery) {

        checkBook(bookQuery);
        //查询图书类型
        log.info("类型名称 : {}",bookQuery.getDictionaryValue());
        DictionaryList dictionaryList = dictionaryListMapper.selectByDictionaryValue("图书类型",bookQuery.getDictionaryValue());

        BookInfo bookInfo = new BookInfo();
        bookInfo.setBookName(bookQuery.getBookName());
        bookInfo.setBookAuthor(bookQuery.getBookAuthor());
        bookInfo.setBookType(dictionaryList.getId());
        bookInfo.setBookPrice(bookQuery.getBookPrice());
        bookInfo.setLanguage(bookQuery.getLanguage());
        bookInfo.setPublisher(bookQuery.getPublisher());
        bookInfo.setPublishDate(bookQuery.getPublishDate());
        bookInfo.setIsValid(1);
        bookInfo.setIntroduction(bookQuery.getIntroduction());

        AssertUtil.isTrue(bookInfoMapper.insertSelective(bookInfo) < 1,"图书添加失败");
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public void updateBook(BookQuery bookQuery) {

        checkBook(bookQuery);

        //查询图书类型
        DictionaryList dictionaryList = dictionaryListMapper.selectByDictionaryValue("图书类型",bookQuery.getDictionaryValue());

        BookInfo bookInfo = new BookInfo();
        bookInfo.setId(bookQuery.getId());
        bookInfo.setBookName(bookQuery.getBookName());
        bookInfo.setBookAuthor(bookQuery.getBookAuthor());
        bookInfo.setBookType(dictionaryList.getId());
        bookInfo.setBookPrice(bookQuery.getBookPrice());
        bookInfo.setLanguage(bookQuery.getLanguage());
        bookInfo.setPublisher(bookQuery.getPublisher());
        bookInfo.setPublishDate(bookQuery.getPublishDate());
        bookInfo.setIntroduction(bookQuery.getIntroduction());

        AssertUtil.isTrue(bookInfoMapper.updateByPrimaryKeySelective(bookInfo) < 1,"图书修改失败");

    }

    /**
     * 检查图书信息是否为空
     * @param bookQuery 前端信息
     */
    private void checkBook(BookQuery bookQuery) {
        AssertUtil.isTrue(StringUtils.isBlank(bookQuery.getBookName()),"书名不能为空");
        AssertUtil.isTrue(StringUtils.isBlank(bookQuery.getBookAuthor()),"作者不能为空");
        AssertUtil.isTrue(StringUtils.isBlank(bookQuery.getDictionaryValue()),"图书类型不能为空");
        AssertUtil.isTrue(StringUtils.isBlank(bookQuery.getPublisher()),"出版商不能为空");
    }

    public BookDTO selectByBookId(Integer id) {
        return bookInfoMapper.selectByBookId(id);

    }

    @Transactional(propagation = Propagation.REQUIRED)
    public void deleteBook(Integer id) {
        AssertUtil.isTrue(id == null || id < 1,"要删除的id不能为空");
        BookInfo bookInfo = new BookInfo();
        bookInfo.setId(id);
        bookInfo.setIsValid(0);
        AssertUtil.isTrue(bookInfoMapper.updateByPrimaryKeySelective(bookInfo) < 1,"删除失败");
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public void deleteBatchBook(Integer[] ids) {

        AssertUtil.isTrue(ids == null || ids.length == 0,"要删除的id不能为空");
        List<BookInfo> list = new ArrayList<>();
        for (Integer id : ids) {
            BookInfo bookInfo = new BookInfo();
            bookInfo.setId(id);
            bookInfo.setIsValid(0);
            list.add(bookInfo);
        }
        AssertUtil.isTrue(bookInfoMapper.updateBatch(list) < 1,"删除图书失败");

    }
    @Transactional(propagation = Propagation.REQUIRED)
    public LendDTO checkRenew(RenewQuery renewQuery) {
        AssertUtil.isTrue(renewQuery.getId() == null,"借阅ID不能为空");
        AssertUtil.isTrue(StringUtils.isBlank(renewQuery.getBookName()),"图书名称不能为空");
        AssertUtil.isTrue(StringUtils.isBlank(renewQuery.getReaderName()),"读者名称不能为空");
        AssertUtil.isTrue(renewQuery.getLendDate() == null,"借阅时间不能为空");

        LendDTO lendDTO = lendListMapper.selectByRenew(renewQuery);
        log.info("查询到的将要续借的书籍 : {}", lendDTO);
        AssertUtil.isTrue(lendDTO == null,"续借的书籍不存在");

        return lendDTO;
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public void renewBook(RenewQuery renewQuery, HttpServletRequest request) {

        LendDTO lendDTO = checkRenew(renewQuery);

        //获取当前管理员
        String admin = CookieUtil.getCookieValue(request, "admin");
        log.info("当前管理员 : {}",admin);
        AssertUtil.isTrue(StringUtils.isBlank(admin),"必须登录后才能续借");

        LendList lendList = lendListMapper.selectByPrimaryKey(lendDTO.getId());
        lendList.setId(renewQuery.getId());
        //设置到期时间 增加一个月
        Calendar instance = Calendar.getInstance();
        instance.setTime(lendDTO.getDueDate());
        instance.add(Calendar.MONTH,1);
        lendList.setDueDate(instance.getTime());
        lendList.setAdminName(admin);
        lendList.setBookStatus("续借");
        lendList.setUpdateTime(LocalDateTime.now());
        AssertUtil.isTrue(lendListMapper.updateByPrimaryKeySelective(lendList) < 1,"借阅表更新失败");

        RenewList renewList = new RenewList();
        renewList.setBookId(lendList.getBookId());
        renewList.setReaderId(lendList.getReaderId());
        //date转化localdatetime
        Instant instant = lendList.getDueDate().toInstant();
        renewList.setDueDate(LocalDateTime.ofInstant(instant, ZoneId.systemDefault()));
        renewList.setCreateDate(LocalDateTime.now());
        renewList.setUpdateDate(LocalDateTime.now());
        renewList.setAdminName(admin);
        renewList.setIsValid(1);
        AssertUtil.isTrue(renewListMapper.insert(renewList) < 1,"续借失败");


    }
}
